package com.android.home.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.android.common.base.rv.BaseMultiMediaGridHolder;
import com.android.common.base.rv.BaseRvAdapter;
import com.android.common.base.rv.BaseRvFragment;
import com.android.common.base.rv.BaseRvHolder;
import com.android.common.mvp.model.Desc;
import com.android.common.utils.media.MediaSelectorUtils;
import com.android.home.R;
import com.android.home.adapter.holder.DescInputHolder;
import com.luck.picture.lib.config.FileSizeUnit;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import java.util.ArrayList;
import java.util.List;

public class VPMediaSelectFragment extends BaseRvFragment<Desc> {

    public static final int SELECT_MAX_MULTIMEDIA_COUNT = 3;
    public static final int SELECT_MAX_VIDEO_COUNT = 3;
    public static final long SELECT_MAX_MULTIMEDIA_SIZE = 200 * FileSizeUnit.MB;
    private BaseRvAdapter<LocalMedia> mMultiMediaAdapter;
    private DescInputHolder mDescInputHolder;

    @Override
    protected boolean onNeedToolBar() {
        return false;
    }

    @Override
    public boolean onEnableLoadMore() {
        return false;
    }

    @Override
    public boolean onEnableRefresh() {
        return false;
    }

    @Override
    protected BaseRvAdapter<Desc> initAdapter() {
        return createMainAdapter();
    }

    private BaseRvAdapter<Desc> createMainAdapter(){
        BaseRvAdapter<Desc> mainAdapter = new BaseRvAdapter<Desc>(this.getContext()) {
            @Override
            protected BaseRvHolder<Desc> createDataViewHolder(ViewGroup parent, int viewType) {
                mDescInputHolder = new DescInputHolder(mLayoutInflater.inflate(R.layout.item_input_media, parent, false)) {
                    @Override
                    protected BaseRvAdapter<LocalMedia> initAdapter(DescInputHolder holder) {
                        return addMultiMediaAdapter(holder);
                    }
                };
                return mDescInputHolder;
            }
        };
        return mainAdapter;
    }

    private BaseRvAdapter<LocalMedia> addMultiMediaAdapter(DescInputHolder addMultiMediaHolder){
        mMultiMediaAdapter = new BaseRvAdapter<LocalMedia>(this.getContext()) {
            @Override
            protected BaseRvHolder<LocalMedia> createDataViewHolder(ViewGroup parent, int viewType) {
                BaseMultiMediaGridHolder addMultiMediaGridHolder = new BaseMultiMediaGridHolder(mLayoutInflater.inflate(R.layout.item_grid_media, parent, false));
                addMultiMediaGridHolder.setChildViewClickListener((view, position) -> {
                    if (view.getId() == R.id.ifv_media) {
                        if (position == getTsSize()) {
                            MediaSelectorUtils.selectMedia(VPMediaSelectFragment.this.getContext(), SelectMimeType.ofAll(), SELECT_MAX_MULTIMEDIA_COUNT, SELECT_MAX_VIDEO_COUNT,SELECT_MAX_MULTIMEDIA_SIZE,this);
                        } else {
                            MediaSelectorUtils.previewMedia(VPMediaSelectFragment.this,position,this);
                        }
                    } else if (view.getId() == R.id.iv_del) {
                        this.remove(position);
                    }
                });
                return addMultiMediaGridHolder;
            }

            @Override
            public int getItemCount() {
                addMultiMediaHolder.setTitleDesc(getTsSize(),addMultiMediaHolder.getLayoutPosition());
                if (getTsSize() < SELECT_MAX_MULTIMEDIA_COUNT) {
                    return getTsSize() + 1;
                } else {
                    return getTsSize();
                }
            }
        };

        return mMultiMediaAdapter;
    }

    @Override
    protected View onCreateRvBottomView() {
        View bottomView = mLayoutInflater.inflate(R.layout.common_bt_bt,null);
        Button bt0 = bottomView.findViewById(R.id.bt_0);
        Button bt1 = bottomView.findViewById(R.id.bt_1);
        bt1.setVisibility(View.GONE);
        bt0.setText(R.string.submit);
        bt0.setOnClickListener(view -> {
            Toast.makeText(mActivity, R.string.submit, Toast.LENGTH_SHORT).show();
        });
        return bottomView;
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        Desc bean = new Desc();
        List<Desc> result = new ArrayList<>();
        result.add(bean);
        refreshOrLoadSuccess(result);
    }
}
